package feizhu_offer40;

import java.util.Arrays;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/6/21 10:40
 * 说明： 取数组中最小的几位元素
 */

public class Mine {
    public int[] getLeastNumbers(int[] arr, int k) {
        int[] res = new int[k];
        if (k == 0) return res;
        if (k == arr.length) return arr;

        sort(arr);

        for (int i = 0; i < k; i++) {
            res[i] = arr[i];
        }
        return res;
    }

    // Bubble Sort
    private void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] >= arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
}
